using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;

namespace CosyTrade.UI
{
    public partial class MainForm : MainFormBase
    {
        private IAddinFormManager addinFormMgr;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Text = Application.ProductName;
            addinFormMgr = (IAddinFormManager) CurrentAddinManager.GetAddin("AddinFormManager");
            ((ThreadStart) InitializeRootMenu).BeginInvoke(null, null);
        }

        private void InitializeRootMenu()
        {
            IList<IMenuItem> rootMenus = addinFormMgr.RootMenus;
            foreach (IMenuItem menu in rootMenus)
            {
                if (menu is SubMenuItem)
                {
                    SubMenuItem rootMenu = (SubMenuItem) menu;
                    NavBarGroup navGroup = new NavBarGroup(rootMenu.Caption);
                    navGroup.SmallImage = rootMenu.Icon;
                    navGroup.Tag = rootMenu;

                    BarSubItem mainMenuGroup = new BarSubItem(barManager1, rootMenu.Caption);
                    mainMenuGroup.Tag = rootMenu;

                    foreach (KeyValuePair<int, IMenuItem> keyValuePair in rootMenu.Items)
                    {
                        CommandMenuItem cmdItem = (CommandMenuItem) keyValuePair.Value;
                        if (cmdItem == null) continue;
                        NavBarItem navItem = new NavBarItem(cmdItem.Caption);
                        navItem.LinkClicked += new NavBarLinkEventHandler(navItem_LinkClicked);
                        navItem.SmallImage = cmdItem.Icon;
                        navItem.Tag = cmdItem;
                        navBarControl1.Items.Add(navItem);
                        navGroup.ItemLinks.Add(navItem);

                        BarButtonItem mainMenuItem = new BarButtonItem(barManager1, cmdItem.Caption);
                        mainMenuItem.ItemClick += new ItemClickEventHandler(mainMenuItem_ItemClick);
                        mainMenuItem.Tag = cmdItem;
                        mainMenuGroup.ItemLinks.Add(mainMenuItem);
                    }

                    Invoke((NavbarControlAdditemCallback)NavbarControlAdditem, navGroup);
                    Invoke((BarMenuAddCallback) barMenuAddGroup, bar2, mainMenuGroup);
                }
            }
        }

        private void mainMenuItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            CommandMenuItem cmdItem = e.Item.Tag as CommandMenuItem;
            if (cmdItem == null) return;

            AddinFormBase addinForm = addinFormMgr.GetAddinForm(cmdItem.PluginKey);
            addinForm.MdiParent = this;
            addinForm.Show();
            addinForm.Activate();
        }

        private void navItem_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            CommandMenuItem cmdItem = e.Link.Item.Tag as CommandMenuItem;
            if (cmdItem == null) return;

            AddinFormBase addinForm = addinFormMgr.GetAddinForm(cmdItem.PluginKey);
            addinForm.MdiParent = this;
            addinForm.Show();
            addinForm.Activate();
        }

        private delegate void NavbarControlAdditemCallback(NavBarGroup navGroup);
        private void NavbarControlAdditem(NavBarGroup navGroup)
        {
            navBarControl1.Groups.Add(navGroup);
            navGroup.Expanded = true;
        }

        private delegate void BarMenuAddCallback(Bar bar, BarItem item);
        private static void barMenuAddGroup(Bar bar, BarItem item)
        {
            bar.ItemLinks.Add(item);
        }
    }
}